/*
 * @Description: http 请求终端
 * @Version: 1.0
 * @Autor: jiajun wu
 * @Date: 2021-11-24 15:21:37
 * @LastEditors: jiajun wu
 * @LastEditTime: 2021-11-24 15:39:18
 */
import { Module } from 'vuex'
import { Canceler } from 'axios'
import { RootStateInterface } from "./rootStateInterface";

interface CancelTokenState {
  httpRequestList: Array<any>,
}

const CancelToken: Module<CancelTokenState, RootStateInterface> = {
  namespaced: false,
  state: {
    httpRequestList: []
  },
  getters: {},
  mutations: {
    ADD_HTTP_REQUEST_LIST(state: CancelTokenState, payload: Canceler | number) {
      if (payload === 0) {
        state.httpRequestList.forEach(item => {
          item("interrupt")
        })
        state.httpRequestList = []
      } else {
        state.httpRequestList.push(payload)
      }
    }
  },
  actions: {
    addHttpRequestList({ commit }, payload: Canceler) {
      commit("ADD_HTTP_REQUEST_LIST", payload)
    },
    async removeHttpRequestList({ commit }) {
      commit("ADD_HTTP_REQUEST_LIST", 0)
    }
  }
}

export default CancelToken;
